using System.IO;
using System.Windows.Forms;

namespace ICP_MS;

public class CommonData
{
	public bool Init()
	{
		if (UserManager.CurrentUser.authority > 1)
		{
			using FormCanType form = new FormCanType();
			form.ShowDialog();
			CAN.Instance.Init(form.CanType);
		}
		else
		{
			string path = Application.StartupPath + "//iMassConfig//Interface.txt";
			int canType = 0;
			using (StreamReader sr = File.OpenText(path))
			{
				canType = int.Parse(sr.ReadLine());
				sr.Close();
			}
			CAN.Instance.Init(canType);
		}
		Detector.Instance.Init();
		LensManager.Instance.Init();
		Plasma.Instance.Init();
		Vacuum.Instance.Init();
		PowerManager.Instance.Init();
		QuadMonitor.Instance.Init();
		Collision.Instance.Init();
		SemiConductor.Instance.Init();
		LoadInstrumentSettings();
		SerialCom.Serial.Init();
		HPLCSerialCom.Serial.Init();
		return false;
	}

	public void Dispose()
	{
		QuadMonitor.Instance.Dispose();
		PowerManager.Instance.Dispose();
		Detector.Instance.Dispose();
		LensManager.Instance.Dispose();
		Plasma.Instance.Dispose();
		Vacuum.Instance.Dispose();
		CAN.Instance.Dispose();
		SerialCom.Serial.Dispose();
		SaveInstrumentSettings();
		AutoSampler.Instance.Deinit();
		Collision.Instance.Dispose();
	}

	private void LoadInstrumentSettings()
	{
		RFS rfs = DataBase.Instance.GetRFS();
		if (rfs != null)
		{
			RFManager.rfs = rfs;
		}
		Quad quad = DataBase.Instance.GetQuad();
		if (quad != null)
		{
			QuadMonitor.quad = quad;
		}
		if (QuadMonitor.quad.Freq < 0 || QuadMonitor.quad.Freq > 2300000)
		{
			QuadMonitor.quad.Freq = 2000000;
		}
		if (QuadMonitor.quad.Temperature < 30 || QuadMonitor.quad.Temperature > 60)
		{
			QuadMonitor.quad.Temperature = 50;
		}
		MassQP qp = DataBase.Instance.GetMassQP();
		if (qp != null)
		{
			RFManager.massQP = qp;
		}
		Power power = DataBase.Instance.GetPowerManager();
		if (power != null)
		{
			PowerManager.powerStatus = power;
		}
		X2Lens lens = DataBase.Instance.GetX2Lens();
		if (lens != null)
		{
			LensManager.x2lens = lens;
		}
		Platform plat = DataBase.Instance.GetPlatform();
		if (plat != null)
		{
			PlatManager.plat = plat;
		}
		PlasmaPara plasma = DataBase.Instance.GetPlasma();
		if (plasma != null)
		{
			PlasmaManager.plasma = plasma;
		}
		InstrumentInjector injector = DataBase.Instance.GetInjector();
		if (injector != null)
		{
			PlatManager.Instrumentinjector = injector;
		}
		PlatManager.injector.Uptake = true;
		PlatManager.injector.Wash = false;
	}

	private void SaveInstrumentSettings()
	{
		RFManager.rfs.name = "last";
		DataBase.Instance.SaveRFS(RFManager.rfs);
		RFManager.massQP.name = "last";
		DataBase.Instance.SaveMassQP(RFManager.massQP);
		QuadMonitor.quad.Name = "last";
		DataBase.Instance.SaveQuad(QuadMonitor.quad);
		LensManager.x2lens.name = "last";
		DataBase.Instance.SaveX2Lens(LensManager.x2lens);
		PowerManager.powerStatus.name = "last";
		DataBase.Instance.SavePowerManager(PowerManager.powerStatus);
		PlatManager.plat.name = "last";
		DataBase.Instance.SavePlatform(PlatManager.plat);
		PlasmaManager.plasma.Name = "last";
		DataBase.Instance.SavePlasma(PlasmaManager.plasma);
		PlatManager.injector.Name = "last";
		DataBase.Instance.SaveInjector(PlatManager.Instrumentinjector);
	}
}
